﻿//HDataMsg_def.h

#ifndef	HDATA_DEF_H_
#define HDATA_DEF_H_

#pragma once

#define	RECV_IMPORT_HDATA_LIST_NUM			10				//
#define	SEND_IMPORT_HDATA_LIST_NUM			5				//

#pragma pack(1)

typedef struct _ImportHData_Cmd
{																	//接收历史数据导入消息
	XBOOL8	bRecved;												//接收到数据标志
	XUINT	SourceNodeId;											//命令源节点ID
	XUINT	SourceProId;											//命令源进程ID
	XUINT	RecvRandomNum;											//问答命令随机码
	XUSHORT	RecvBytes;												//接收数据长度
	XUCHAR	DataBuff[NSA3000T_NET_MSG_MIX_CELL];					//接收缓冲区
	_ImportHData_Cmd()
	{
		bRecved = false;
		SourceNodeId = 0;
		SourceProId = 0;
		RecvRandomNum = 0;
		RecvBytes = 0;
		memset(DataBuff, 0, NSA3000T_NET_MSG_MIX_CELL);
	}
	
	void Clear()
	{
		if (bRecved)
		{
			bRecved = false;
		}
		SourceNodeId = 0;
		SourceProId = 0;
		RecvRandomNum = 0;
		if (RecvBytes)
			RecvBytes = 0;
		memset(DataBuff, 0, NSA3000T_NET_MSG_MIX_CELL);
	}
	
}ImportHData_Cmd;													//接收历史数据导入消息

typedef struct _ImportHData_Unit
{
	XINT			 Index;
	ImportHData_Cmd  CmdData;
	_ImportHData_Unit()
	{
		Index=0;
	}
}ImportHData_Unit;

typedef struct _ImportHData_Chain
{
	XUSHORT			   MsgNum;
	XUSHORT			   ReadPos;
	XUSHORT			   WritePos;
	ImportHData_Unit   RecvCmd[RECV_IMPORT_HDATA_LIST_NUM];
	_ImportHData_Chain()
	{
		MsgNum=0;
		ReadPos=0;
		WritePos=0;
		for (XUSHORT i=0; i<RECV_IMPORT_HDATA_LIST_NUM; i++ )
		{
			RecvCmd[i].Index = i;
		}
	}

	XBOOL8 IsFull()
	{
		if (MsgNum>0 && ReadPos == WritePos)
		{
			return true;
		}else
		{
			return false;
		}
	}

	XBOOL8 IsFree()
	{
		if (0 == MsgNum && ReadPos == WritePos)
		{
			return true;
		}else
		{
			return false;
		}
	}
	XUSHORT NextPos(XUSHORT CurPos)
	{
		return (CurPos+1)%RECV_IMPORT_HDATA_LIST_NUM;
	}
	XINT ReadOne(ImportHData_Cmd &ImportCmd)
	{
		if (IsFree())
		{
			return 0;
		}
		XUSHORT TempPos=ReadPos;
		if (RecvCmd[TempPos].CmdData.bRecved &&
			RecvCmd[TempPos].CmdData.RecvBytes>0)
		{
			memcpy((XCHAR*)&ImportCmd, (XCHAR*)&RecvCmd[TempPos].CmdData, sizeof(ImportHData_Cmd));
		}
		ReadPos = NextPos(TempPos);
		if (MsgNum>0)
		{
			MsgNum--;
		}
		return 1;
	}
	XINT WriteOne(ImportHData_Cmd ImportCmd)
	{
		if (IsFull())
		{
			return 0;
		}
		XUSHORT TempPos = WritePos;
		if (ImportCmd.bRecved &&
			ImportCmd.RecvBytes>0)
		{
			memcpy((XCHAR*)&RecvCmd[TempPos].CmdData, (XCHAR*)&ImportCmd, sizeof(ImportHData_Cmd));
		}
		WritePos = NextPos(TempPos);
		
		MsgNum = (MsgNum+1)%(RECV_IMPORT_HDATA_LIST_NUM+1);
		
		return 1;
	}
}ImportHData_Chain;										//接收历史数据导入消息链表

typedef struct _ImportHD_Cmd_Send
{																	//发送历史数据导入消息
	XBOOL8	NeedWait;												//需要等待标志
	XBOOL8	bSend;													//到数据标志
	XINT	DstNodeId;												//目的节点
	XINT	DstProcId;												//目的进程
	XUSHORT	SendBytes;												//发送数据长度
	XUCHAR	ToBuff[NSA3000T_NET_MSG_MIX_CELL];						//发送缓冲区
	_ImportHD_Cmd_Send()
	{
		NeedWait = false;
		bSend = false;
		SendBytes = 0;
		memset(ToBuff, 0, NSA3000T_NET_MSG_MIX_CELL);
	}

	void Clear()
	{
		if (bSend)
		{
			bSend = false;
		}
		NeedWait = false;
		DstNodeId=0;
		DstProcId=0;
		if (SendBytes)
			SendBytes = 0;
		memset(ToBuff, 0, NSA3000T_NET_MSG_MIX_CELL);
	}

}ImportHD_Cmd_Send;													//发送历史数据导入消息

typedef struct _ImportHD_Send_Unit
{
	XINT				Index;
	ImportHD_Cmd_Send   SendCmdData;
	_ImportHD_Send_Unit()
	{
		Index=0;
	}
}ImportHD_Send_Unit;

typedef struct _ImportHD_Send_Chain
{
	XBOOL8				 bReading;
	XBOOL8				 bWritting;
	XUSHORT				 MsgNum;
	XUSHORT			     ReadPos;
	XUSHORT				 WritePos;
	ImportHD_Send_Unit   SendCmd[SEND_IMPORT_HDATA_LIST_NUM];
	_ImportHD_Send_Chain()
	{
		bReading=false;
		bWritting=false;
		MsgNum=0;
		ReadPos=0;
		WritePos=0;
		for (XUSHORT i=0; i<SEND_IMPORT_HDATA_LIST_NUM; i++ )
		{
			SendCmd[i].Index = i;
		}
	}
	
	XBOOL8 IsFull()
	{
		if (MsgNum>0 && ReadPos == WritePos)
		{
			return true;
		}else
		{
			return false;
		}
	}

	XBOOL8 IsFree()
	{
		if (0 == MsgNum && ReadPos == WritePos)
		{
			return true;
		}else
		{
			return false;
		}
	}
	XUSHORT NextPos(XUSHORT CurPos)
	{
		return (CurPos+1)%SEND_IMPORT_HDATA_LIST_NUM;
	}
	XINT ReadOne(ImportHD_Cmd_Send &ImportCmd)
	{
		if (IsFree())
		{
			return 0;
		}
		XUSHORT TempPos=ReadPos;
		if (SendCmd[TempPos].SendCmdData.bSend &&
			SendCmd[TempPos].SendCmdData.SendBytes>0)
		{
			memcpy((XCHAR*)&ImportCmd, (XCHAR*)&SendCmd[TempPos].SendCmdData, sizeof(ImportHD_Cmd_Send));
		}
		ReadPos = NextPos(TempPos);
		if (MsgNum>0)
		{
			MsgNum--;
		}
		return 1;
	}
	XINT WriteOne(ImportHD_Cmd_Send ImportCmd)
	{
		if (IsFull())
		{
			return 0;
		}
		XUSHORT TempPos = WritePos;
		if (ImportCmd.bSend &&
			ImportCmd.SendBytes>0)
		{
			memcpy((XCHAR*)&SendCmd[TempPos].SendCmdData, (XCHAR*)&ImportCmd, sizeof(ImportHD_Cmd_Send));
		}
		WritePos = NextPos(TempPos);

		MsgNum = (MsgNum+1)%(SEND_IMPORT_HDATA_LIST_NUM+1);

		return 1;
	}
}ImportHD_Send_Chain;										//接收历史数据导入消息链表

typedef struct _Local_Import_Cmd
{
	XUSHORT			ImportTableNum;
	XUSHORT			LastImportPos;
	ImportHData_Cmd RecvCmd;
	_Local_Import_Cmd()
	{
		ImportTableNum=0; 
		LastImportPos=0;
		RecvCmd.Clear();
	}
	XBOOL8 IsFree()
	{
		if (RecvCmd.bRecved && RecvCmd.RecvBytes>0)
		{
			return false;
		}else
		{
			return true;
		}
	}
	void Clear()
	{
		ImportTableNum = 0;
		LastImportPos = 0;
		RecvCmd.Clear();
	}

}Local_Import_Cmd;

#pragma pack()

//////////////////////////////////////////////////////////////////////////
#endif